package com.wzx.goods.controller;

import com.wzx.entity.Result;
import com.wzx.entity.StatusCode;
import com.wzx.goods.pojo.Brand;
import com.wzx.goods.service.BrandService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/brands")
@Slf4j
public class BrandController {
    @Autowired
    BrandService brandService;

    //@RequestMapping(path = "",method = {RequestMethod.GET})
    @GetMapping //是上面注解的简化
    public Object getAll() {

        List<Brand> list = brandService.findAll();

        return new Result<Brand>(true, StatusCode.OK, "查询成功", list);
    }

    @GetMapping(path = "/{id}")
    public Object getOne(@PathVariable Integer id) {

        Brand brand = brandService.findById(id);

        return new Result<Brand>(true, StatusCode.OK, "查询成功", brand);
    }

    @DeleteMapping(path = "/{id}")
    public Object deleteOne(@PathVariable Integer id) {

        brandService.deleteById(id);

        return new Result<Brand>(true, StatusCode.OK, "删除成功");
    }
    @PostMapping
    public Object addOne(@RequestBody  Brand brand) {//@RequestBody将接收到json中的数据存到brand对象

        brandService.saveBrand(brand);

        return new Result<Brand>(true, StatusCode.OK, "添加成功");
    }
    @PutMapping
    public Object updateOne(@RequestBody  Brand brand) {

        brandService.updateBrand(brand);

        return new Result<Brand>(true, StatusCode.OK, "修改成功");
    }
}